package com.hengtiansoft.lfy.widget;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hengtiansoft.lfy.LFYApplication;
import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.db.been.InterpretRecordDB;
import com.hengtiansoft.lfy.entity.CollectEntity;
import com.hengtiansoft.lfy.entity.ServiceResult;
import com.hengtiansoft.lfy.net.NetHelper;
import com.hengtiansoft.lfy.net.NetUtils;
import com.hengtiansoft.lfy.utils.Kdxf_TTSPlayer;
import com.j256.ormlite.stmt.QueryBuilder;

import java.sql.SQLException;
import java.util.List;

/**
 * 自定义对话框
 */
public class TranslateDialog extends Dialog {

    public TranslateDialog(Context context) {
        super(context);
    }

    public TranslateDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String chinese;
        private String english;
        private boolean isCollect = false;
        private TextView tv_collect;
        private int serverId = -1;
        private int id = -1;

        public Builder(Context context, String english, String chinese) {
            this.context = context;
            this.english = english;
            this.chinese = chinese;
        }

        public Builder setContentView(View v) {
            return this;
        }

        public TranslateDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final TranslateDialog dialog = new TranslateDialog(context, R.style.myDialogTheme);
            View v = inflater.inflate(R.layout.dialog_translate, null);
            final TextView en = (TextView) v.findViewById(R.id.tv_english);
            TextView ch = (TextView) v.findViewById(R.id.tv_chinese);

            TextView tv_play = (TextView) v.findViewById(R.id.eva_play);
            tv_collect = (TextView) v.findViewById(R.id.eva_collect);

            tv_collect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   collect();
                }
            });
            tv_play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Kdxf_TTSPlayer.getInstense().TTSPlay(english,"","");
                }
            });

            en.setText(english);
            ch.setText(chinese);

            dialog.setCanceledOnTouchOutside(true);
            dialog.setContentView(v);
            insertDataForInterpretRecordDB();
            return dialog;
        }


        //执行收藏/反收藏
        public void collect(){
            int mode = -1;
            if (isCollect) {
                mode = 1;//取消收藏
            }else{
                mode = 0;//添加收藏
            }
            final int mMode = mode;
            NetHelper.collect(context, mode, english, chinese, serverId+"", new NetUtils.NetCallBack<ServiceResult>() {
                @Override
                public void success(ServiceResult rspData) {
                    CollectEntity entity = (CollectEntity)rspData;
                    if ( entity.getRetcode() == 0) {
                        if( mMode == 0 ){
                            //添加收藏成功
                            double data = (Double) entity.getData();
                            serverId = (int)data; //服务器返回serverId
                            isCollect = true;
                            Toast.makeText(context, context.getString(R.string.collect_success), Toast.LENGTH_LONG).show();
                            tv_collect.setBackgroundResource(R.drawable.btn_collection_selected);
                        }else{
                            //取消收藏成功
                            serverId = -1;
                            isCollect = false;
                            Toast.makeText(context, context.getString(R.string.uncollect_success), Toast.LENGTH_SHORT).show();
                            tv_collect.setBackgroundResource(R.drawable.btn_collection);
                        }
                        updateDataForInterpretRecordDB();
                    }else{
                        Toast.makeText(context, entity.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void failed(String msg) {
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                }
            }, CollectEntity.class);
        }

        /**
         * 修改收藏状态
         */
        public void insertDataForInterpretRecordDB() {
            String username = "robot";
            InterpretRecordDB interpretRecordDB = new InterpretRecordDB();
            interpretRecordDB.setUsername(username);
            interpretRecordDB.setText(english);
            interpretRecordDB.setTranslation(chinese);
            interpretRecordDB.setIscollected("N");
            interpretRecordDB.setFilename("");
            interpretRecordDB.setServerId(-1);

            if (null != LFYApplication.getHelper(context)
                    .getInterpretRecordDao()) {
                LFYApplication.getHelper(context)
                        .getInterpretRecordDao().create(interpretRecordDB);

                try {
                    @SuppressWarnings("rawtypes") //ormlite框架 删除数据
                            QueryBuilder builder = LFYApplication.getHelper(context)
                            .getInterpretRecordDao().queryBuilder();
                    builder.where().eq("text", english);
                    List<InterpretRecordDB> mInterpretRecordDBList = builder.query();
                    if(mInterpretRecordDBList != null && mInterpretRecordDBList.size() > 0){
                        id = mInterpretRecordDBList.get(0).getId();
                    }else{
                        Log.d("tag","no data");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
        }

        /**
         * 修改收藏状态
         */
        public void updateDataForInterpretRecordDB() {
            if(id == -1)
                return;;
            InterpretRecordDB interpretRecordDB1 = new InterpretRecordDB();
            interpretRecordDB1.setId(id);
            List<InterpretRecordDB> mInterpretRecordDBList = null;
            if (null != LFYApplication.getHelper(context)
                    .getInterpretRecordDao()) {
                try {
                    @SuppressWarnings("rawtypes")
                    QueryBuilder builder = LFYApplication.getHelper(context)
                            .getInterpretRecordDao().queryBuilder();
                    builder.where().eq("id", id);
                    mInterpretRecordDBList = builder.query();
                } catch (SQLException e) {
                    mInterpretRecordDBList = null;
                }
            }
            if (null != mInterpretRecordDBList
                    && mInterpretRecordDBList.size() > 0) {
                InterpretRecordDB interpretRecordDB = new InterpretRecordDB();
                interpretRecordDB.setId(id);
                if(isCollect)
                    interpretRecordDB.setIscollected("Y");
                else
                    interpretRecordDB.setIscollected("N");
                interpretRecordDB.setText(mInterpretRecordDBList.get(0).getText());
                interpretRecordDB.setTranslation(
                        mInterpretRecordDBList.get(0).getTranslation());
                interpretRecordDB
                        .setUsername(mInterpretRecordDBList.get(0).getUsername());
                interpretRecordDB
                        .setFilename(mInterpretRecordDBList.get(0).getFilename());
                interpretRecordDB.setServerId(serverId);
                LFYApplication.getHelper(context).getInterpretRecordDao()
                        .update(interpretRecordDB);
            }
        }


    }

}